package com.mzy.leetcode;

import com.mzy.ListNode;

public class AddTwoNumbers {


    public static class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            int l1Len = listLength(l1);
            int l2Len = listLength(l2);

            ListNode minListNode = l1Len < l2Len ? l1 : l2;
            ListNode maxListNode = l1 == minListNode ? l2 : l1;
            l2 = maxListNode;

            ListNode last = maxListNode;

            int num = 0;

            int carry = 0;

            while (minListNode != null) {
                num = (minListNode.val + maxListNode.val + carry) % 10;
                carry = (minListNode.val + maxListNode.val + carry) / 10;
                maxListNode.val = num;
                last = maxListNode;
                minListNode = minListNode.next;
                maxListNode = maxListNode.next;
            }

            while (maxListNode != null) {
                num = (maxListNode.val + carry) % 10;
                carry = (maxListNode.val + carry) / 10;
                maxListNode.val = num;

                last = maxListNode;
                maxListNode = maxListNode.next;
            }

            if (carry == 1) {
                last.next = new ListNode(1);
            }
            return l2;
        }

        private int listLength(ListNode l1) {
            int length = 0;
            while (l1 != null) {
                l1 = l1.next;
                length++;
            }
            return length;
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        ListNode listNode = new ListNode(3);
        listNode.next = new ListNode(7);
        listNode.next.next = new ListNode(3);

        ListNode listNode1 = new ListNode(9);
        listNode1.next = new ListNode(2);
        listNode1.next.next = new ListNode(4);
        System.out.println();
        // System.out.println(solution.listLength(listNode));

        // System.out.println(solution.addTwoNumbers(listNode1, listNode));

        ListNode result = addTowNumber(listNode1, listNode);
        while (result != null) {
            System.out.println(result.val);
            result = result.next;
        }
    }

    public static ListNode addTowNumber(ListNode l1, ListNode l2) {
        Solution solution = new Solution();
        int len1 = solution.listLength(l1);
        int len2 = solution.listLength(l2);
        ListNode maxListNode = len1 > len2 ? l1 : l2;
        ListNode minListNode = maxListNode == l1 ? l2 : l1;
        l1 = maxListNode;
        int carry = 0;
        ListNode last = maxListNode;
        while (minListNode != null) {
            int sum = minListNode.val + maxListNode.val + carry;
            maxListNode.val = sum % 10;
            carry = sum / 10;
            last = maxListNode;
            minListNode = minListNode.next;
            maxListNode = maxListNode.next;
        }

        while (maxListNode != null) {
            int sum = maxListNode.val + carry;
            maxListNode.val = sum % 10;
            carry = sum / 10;
            last = maxListNode;
            maxListNode = maxListNode.next;

        }
        if(carry == 1) {
            last.next = new ListNode(1);
        }
        return l1;
    }
}
